package com.example.aiinterview.controller;

import com.example.aiinterview.service.AIService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/test")
@RequiredArgsConstructor
public class TestController {

    private final AIService aiService;

    @Value("${deepseek.api.api-key}")
    private String apiKey;

    @Value("${deepseek.api.url}")
    private String apiUrl;

    @GetMapping("/ai-page")
    public String aiTestPage() {
        return "ai-test";
    }

    @GetMapping("/chat-test")
    public String chatTestPage() {
        return "chat-test";
    }

    @GetMapping("/api/ai")
    @ResponseBody
    public ResponseEntity<Map<String, Object>> testAI() {
        Map<String, Object> response = new HashMap<>();
        
        try {
            // 测试配置
            response.put("apiUrl", apiUrl);
            response.put("apiKeyConfigured", apiKey != null && !apiKey.trim().isEmpty());
            response.put("apiKeyPrefix", apiKey != null ? apiKey.substring(0, Math.min(10, apiKey.length())) + "..." : "null");
            
            // 测试AI服务
            String testPrompt = "你好，请简单回复'测试成功'";
            String aiResponse = aiService.generateResponse(testPrompt);
            
            response.put("testPrompt", testPrompt);
            response.put("aiResponse", aiResponse);
            response.put("success", true);
            
        } catch (Exception e) {
            response.put("success", false);
            response.put("error", e.getMessage());
            response.put("errorType", e.getClass().getSimpleName());
        }
        
        return ResponseEntity.ok(response);
    }
}
